﻿using System;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Windows.Forms;
using System.Xml.Linq;
using DevExpress.Xpo;
using Microsoft.CSharp;

namespace Contacts.Data
{
    public class Unknown : XPObject
    {
        public Unknown()
            : base()
        {

        }

        public Unknown(Session session)
            : base(session)
        {

        }

        [DisplayName("First Name")]
        private string _FirstName;
        public string FirstName
        {
            get
            {
                return _FirstName;
            }
            set
            {
                SetPropertyValue("FirstName", ref _FirstName, value);
            }
        }
    }



}
public class DynamicClass
{

    public dynamic GetDynamicCls()
    {
        ICodeCompiler loCompiler = new CSharpCodeProvider().CreateCompiler();

        CompilerParameters loParameters = new CompilerParameters();

        loParameters.ReferencedAssemblies.Add("System.dll");

        loParameters.ReferencedAssemblies.Add("DevExpress.Xpo.v11.1.dll");
        loParameters.ReferencedAssemblies.Add("DevExpress.Data.v11.1.dll");

        string lcCode = @"using System;

                        using System.IO;

                        using DevExpress.Xpo;
                        
 

                        namespace Contacts.Data {

                       public class Unknown1 : XPObject
                        {
                            public Unknown1()
                                : base()
                            {
            
                            }

                            public Unknown1(Session session)
                                : base(session)
                            {
            
                            }";


        var doc = XDocument.Load("GenSchema.xml");
        var properties = doc.Element("Properties").Elements("Property");
        foreach (var xElement in properties)
        {
            string name = xElement.Element("Name").Value;
            string displayname = xElement.Element("DisplayName").Value;
            string dataType = xElement.Element("DataType").Value;
            string prop = @"[DisplayName(" + "\"" + displayname + "\"" + @")]
                            private " + dataType + " _" + name
                            + "; public " + dataType + " " + name
                            + @"{
                                get
                                {
                                    return _" + name + @";
                                }
                                set
                                {
                                    SetPropertyValue(" + "\"" + name + "\"" + @", ref _" + name + @", value);
                                }
                            }  ";
            lcCode += prop;
        }

        lcCode += "}}";
        

        loParameters.GenerateInMemory = false;

        CompilerResults loCompiled =

                loCompiler.CompileAssemblyFromSource(loParameters, lcCode);



        if (loCompiled.Errors.HasErrors)
        {

            string lcErrorMsg = "";



            lcErrorMsg = loCompiled.Errors.Count.ToString() + ") Errors:";

            for (int x = 0; x < loCompiled.Errors.Count; x++)

                lcErrorMsg = lcErrorMsg + "\r\nLine: " +

                             loCompiled.Errors[x].Line.ToString() + " - " +

                             loCompiled.Errors[x].ErrorText;



            MessageBox.Show(lcErrorMsg + "\r\n\r\n" + lcCode,

                            "Compiler Demo");

            return null;

        }



        Assembly loAssembly = loCompiled.CompiledAssembly;
        dynamic loObject = loAssembly.CreateInstance("Contacts.Data.Unknown1");

        if (loObject == null)
        {

            MessageBox.Show("Couldn't load class.");

            return null;

        }
        return loObject;
    }
}